
const { ccclass, menu, property } = cc._decorator;
/**
 * 线性插值辅助工具,计算比例 
 */
@ccclass
@menu("自定义组件/线性插值工具")
export default class LerpHelpTool extends cc.Component {


    _tt = 0;
    _markT = 0;
    _callback = null;
    /** 开始线性插值
     * 
     * @param totalTime  总执行时间 s
     * @param callback 返回比例
     */
    startLerp(totalTime: number, callback: (rate) => void) {
        this._tt = totalTime;
        this._callback = callback;
    }
    /** 停止插值
     * 
     * @param rate 有值时在停止前会再调用一次回调
     */
    stopLerp(rate = 0) {
        this._tt = 0
        this._markT = 0
        if (this._callback) {
            this._callback(rate)
            this._callback = null;
        }
    }
    update(dt) {
        if (this._tt > 0) {
            this._markT += dt;
            let rate = this._markT / this._tt
            if (rate > 1) {
                rate = 1;
                this.stopLerp(rate)
                return;
            } else {
                this._callback(rate)
            }
        }
    }
}
